jQuery(document).ready(function() {
	
	if($('#mcs_container').length){
		$("#mcs_container").mCustomScrollbar("vertical",600,"easeOutCirc",1.05,"auto","yes","yes",10);
	//	$("#mcs4_container").mCustomScrollbar("vertical",200,"easeOutCirc",1.25,"fixed","yes","no",0);
	}
	self.scrollTo(0,0);
	
	$('#mcs_container .customScrollBox .content').css('min-height', $('#mcs_container .content').outerHeight(true)+1000 );
	
	if (sessionStorage.screenStatus && sessionStorage.screenStatus == 'large'){
		expandScreen();
	}else{
		collapseScreen();
	}
	
	display_search_match();
	display_greeting();
	
	setInterval('cleaning()',60000);
	
	//update_height();
	 
	if (typeof act_sys == "string") {
		if(act_sys == 'search'){
			$('#header-nav .first').addClass('act');
			if (!sessionStorage.screenStatus || sessionStorage.screenStatus != 'large'){
				self.scrollTo(400,400);
			}
		}
		if( act_sys == 'homepage'){
			$('#header-nav .first').addClass('act');
		}
	}
	
	$('.btn_cancel,.btn_ok').bind('click',function(){window.location= BASE_URI+'ranking';});
});

function display_greeting(){
	if(json_AUTHOR != null){ 
		$('#name').html( json_AUTHOR.user_login_greeting + json_AUTHOR.name );
	}else{
		$('#name').html('');
	}
}

function cleaning(){
	jQuery.post(BASE_URI+'ranking/aj_cleaning_file');
}

document.onkeydown = function(event) {
	if (event.keyCode == 27) { 
		if(sessionStorage.screenStatus && sessionStorage.screenStatus == 'large'){
			collapseScreen();
		}
	}
}


function toogleScreen(){
	if (!sessionStorage.screenStatus || sessionStorage.screenStatus == 'small'){
		expandScreen();
	}else{
		collapseScreen();
	} 
}

function expandScreen(){
	sessionStorage.setItem('screenStatus','large');
	if(! $('#mcs_container').length){ return false;}
	
	$('#header').hide();
	$('#main-feature').hide();
//	$('#search-area').hide();	
	$('#news-content').hide();
	$('#footer').hide();
	
	$('#mcs_container').css('height',1300);
	$('#mcs_container .customScrollBox .content').css('min-height',$('#mcs_container .content').outerHeight(true) > 1000 ? $('#mcs_container .content').outerHeight(true)+1000 : 1500 );
	$('#mcs_container .dragger_container').css('height',1300);
	$('#vertical-align').css('height',1301);
	$('#ranking-zone').css('height',1400);
	
	$('#display_status_screen').val(normal_screen_stt);
	self.scrollTo(0,0);	
	$("#mcs_container").mCustomScrollbar("vertical",600,"easeOutCirc",1.05,"auto","yes","yes",10);
	
//	display_search_match();
}

function collapseScreen(){
	sessionStorage.setItem('screenStatus','small');
	if(! $('#mcs_container').length){ return false;}
	
	$('#header').show();
	$('#main-feature').show();
//	$('#search-area').show();	
	$('#news-content').show();
	$('#footer').show();
	
	$('#mcs_container').css('height',762);
	$('#mcs_container .customScrollBox .content').css('min-height', $('#mcs_container .content').outerHeight(true)+1000 );
	$('#mcs_container .dragger_container').css('height',735);
	$('#vertical-align').css('height',763);
	$('#ranking-zone').css('height',863);
	
	$('#display_status_screen').val(wide_screen_stt);
	//self.scrollTo(400,400);
	$("#mcs_container").mCustomScrollbar("vertical",600,"easeOutCirc",1.05,"auto","yes","yes",10);
	
//	display_search_match();
}

function update_height(){
	$newHeight = $('#mcs_container .row').outerHeight(true);
	$oldHeight = $('#mcs_container .content').outerHeight(true);
	
	if($newHeight >= $oldHeight){
		$('#mcs_container .content').css('min-height',$newHeight+300);
	}
	$("#mcs_container").mCustomScrollbar("vertical",600,"easeOutCirc",1.05,"auto","yes","yes",10);
}

function display_search_match(){
	if($('.first_match').length){
		var distance = distance_e1_e2( '.first_match','#rank-header');
		if(distance > 155){
			var top = -1 * (distance-40);
			$('#mcs_container .container').css('top',top);
			$("#mcs_container").mCustomScrollbar("vertical",600,"easeOutCirc",1.05,"auto","yes","yes",10);
		}
	}
}



function distance_e1_e2(e1, e2){
	var o1 = $(e1).offset();
	var o2 = $(e2).offset();
	var dx = o1.left - o2.left;
	var dy = o1.top - o2.top;
	//var distance = Math.sqrt(dx * dx + dy * dy);
	var distance = Math.abs(dy);
	return distance;
}

function width_of($what){
	return Math.ceil( $($what).width() );
}

function for_ie(){
	if( $.browser.msie ){
		var left =  Math.ceil ( ( width_of(document) - width_of('.rank_alert') ) /2 );
		$('.rank_alert').offset({'left':left});
	}
}






